﻿/*
	Copyright (c) 2013 Andrew Meyer

	Permission is hereby granted, free of charge, to any person obtaining a copy of this software
	and associated documentation files (the "Software"), to deal in the Software without
	restriction, including without limitation the right to use, copy, modify, merge, publish,
	distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all copies or
	substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
	BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
	DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using Ascend;

namespace AscendViewer
{
	public class BoneContentNodeViewModel : ContentNodeViewModel
	{
		#region Lifecycle
		public BoneContentNodeViewModel(Bone bone, Queue<TransformableObject> axisSceneAliases,
			Queue<TransformableObject> boneSceneAliases, ContentExplorerViewModel container,
			ContentNodeViewModel parent)
			: base(bone, axisSceneAliases, boneSceneAliases, container, parent)
		{
			Name = bone.Name;

			Armature = (Parent is ArmatureContentNodeViewModel)
				? (ArmatureContentNodeViewModel)Parent
				: ((BoneContentNodeViewModel)Parent).Armature;

			InitializeChildren(
				bone.RigidAttachments.Cast<TransformableObject>().Concat(
					bone.Children.Cast<TransformableObject>()),
				axisSceneAliases, boneSceneAliases, container);

			// Create axes overlays
			_axesNormal = CreateAxes(scale: 0.5);
			_axesSelected = CreateAxes(0.05, 0.5);

			double length = ((Bone)Data).Children.Any()
					? ((Bone)Data).Children.First().Z
					: 1.0;

			// Create bone overlay
			_boneVisual = CreateBoneVisual(length, Colors.LightGray);
			_boneVisual.MouseDoubleClick += (sender, e) =>
				{
					// Select this content node
					_container.SelectedContentNode = this;

					// Point camera to node
					_container.MainWindow.MainScene.LookAt(Data);

					// Mark as handled so ancestors are not selected
					e.Handled = true;
				};
		}
		#endregion

		#region Private Members
		private ModelSceneNode _axesNormal;
		private ModelSceneNode _axesSelected;
		private ModelSceneNode _boneVisual;
		#endregion

		#region Public Properties
		public ArmatureContentNodeViewModel Armature
		{
			get;
			private set;
		}

		private OverlayState _axisOverlayState;
		public OverlayState AxisOverlayState
		{
			get
			{
				return _axisOverlayState;
			}

			set
			{
				if (_axisOverlayState == value)
				{
					return;
				}

				// Remove old axes, if any
				if (_axisOverlayState != OverlayState.Hidden)
				{
					_axesNormal.BoneParent = null;
					_axesSelected.BoneParent = null;
				}

				switch (value)
				{
					// Replace with normal axes
					case OverlayState.Normal:
						_axesNormal.BoneParent = (Bone)_axisSceneAlias;
						break;

					// Replace axes with bold axes
					case OverlayState.Selected:
						_axesSelected.BoneParent = (Bone)_axisSceneAlias;
						break;
				}

				_axisOverlayState = value;
			}
		}

		private OverlayState _boneOverlayState;
		public OverlayState BoneOverlayState
		{
			get
			{
				return _boneOverlayState;
			}

			set
			{
				if (_boneOverlayState == value)
				{
					return;
				}

				SolidColorBrush brush =
					(SolidColorBrush)((DiffuseMaterial)_boneVisual.Model.Material).Brush;

				switch (value)
				{
					// Hide bone
					case OverlayState.Hidden:
						_boneVisual.BoneParent = null;
						break;

					// Make sure bone is visible and gray
					case OverlayState.Normal:
						_boneVisual.BoneParent = (Bone)_boneSceneAlias;
						brush.Color = Colors.LightGray;
						break;

					// Make sure bone is visible and highlight
					case OverlayState.Selected:
						_boneVisual.BoneParent = (Bone)_boneSceneAlias;
						brush.Color = Colors.Magenta;
						break;
				}

				_boneOverlayState = value;
			}
		}
		#endregion

		#region Protected Methods
		protected override void HandleSelectionChange()
		{
			base.HandleSelectionChange();

			if (IsSelected)
			{
				AxisOverlayState = OverlayState.Selected;
				BoneOverlayState = OverlayState.Selected;
			}
			else
			{
				AxisOverlayState = OverlayState.Normal;
				BoneOverlayState = OverlayState.Normal;
			}
		}
		#endregion
	}
}
